<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2019/12/3
 * Time: 11:53
 */

namespace sys;

/**
 * 简单的日志类
 * @package sys
 */
class Log
{
    use Singleton;

    // 写入日志
    public function write($content = ''){
        // 文件路径
        $log_path = RUNTIME_PATH . "log" . DS . date("Ymd") . ".log";

        $time = date("Y-m-d H:i:s");
        $file_path = $_SERVER['PHP_SELF'];
        $ip = $ip = $_SERVER['REMOTE_ADDR'] ?? "未知";
        $content = json_encode($content) . PHP_EOL . str_pad('',50,"--");
        // 写入内容
        $log = <<<LOG
        
    地址：$ip
    时间：$time
    地址：$file_path
    记录：$content
LOG;

        // 创建 log 文件
        if(!is_file($log_path)){
            $log_dir = RUNTIME_PATH . "log";

            !is_dir($log_dir) && mkdir(iconv("UTF-8", "GBK",$log_dir), 0777, true);

            file_put_contents( $log_path,str_pad('',50,"--") . $log);
        }
        else{
            $log = file_get_contents($log_path) . $log;
            file_put_contents($log_path,$log);
        }
        return true;
    }
}